package com.cy.demo.service.impl;

import com.cy.demo.mapper.AddressMapper;
import com.cy.demo.mapper.CartMapper;
import com.cy.demo.pojo.Address;
import com.cy.demo.pojo.Cart;
import com.cy.demo.service.IAddressService;
import com.cy.demo.service.ICartService;
import com.cy.demo.service.IDistrictService;
import com.cy.demo.service.ex.AccessDeniedException;
import com.cy.demo.service.ex.AddressCountLimitException;
import com.cy.demo.service.ex.AddressNotFoundException;
import com.cy.demo.service.ex.InsertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class IAddressServiceImpl implements IAddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private IDistrictService districtService;

    //配置文件application.properties中的 maxCount
    @Value("${user.address.max-count}")
    int maxCount;



    @Override
    public void addNewAddress(int uid, String username, Address address) {
        //当前时间
        Date date = new Date();
        //当前总记录数
        int count = addressMapper.countByUid(uid);

        if (count >= maxCount){
            //已达上限 不允许再添加
            System.err.println( "up to the max limit!");
            throw new AddressCountLimitException("用户收货地址条数超出上限！");
        }else {
            // uid  isDefault的设置
            int isDefault = count == 0 ? 1 : 0;
            address.setIsDefault(isDefault);
            address.setUid(uid);
            //地址数据的填充(省市区)
            String provinceName = districtService.getNameByCode(address.getProvinceCode());
            String cityName = districtService.getNameByCode(address.getCityCode());
            String areaName = districtService.getNameByCode(address.getAreaCode());
            address.setProvinceName(provinceName);
            address.setCityName(cityName);
            address.setAreaName(areaName);
            //四项日志
            address.setModifiedTime(date);
            address.setCreatedTime(date);
            address.setModifiedUser(username);
            address.setCreatedUser(username);

            int rows =  addressMapper.insert(address);

            if (rows != 1){
                throw new InsertException("插入时发生异常");
            }
        }
    }

    @Override
    public List<Address> showAddressesByUid(int uid) {
        return addressMapper.findByUid(uid);
    }

    @Override
    public void setDefault(int aid, int uid, String username) {
        if (addressMapper.findByAid(aid) != null){
            //找到了地址，不为空
            addressMapper.SetOtherNoDefaultByUid(uid);
            addressMapper.SetDefaultByAid(aid,username,new Date());
        }else {
            throw new AddressNotFoundException("地址不存在！");
        }
    }

    @Override
    public void deleteByAid(int aid) throws Exception {

        int rows = addressMapper.deleteByAid(aid);

        if (rows == 0){
            throw new Exception("Delete Exception!");
        }
    }

    @Override
    public void updateByAid(String username, Address address) {
        //当前时间
        Date date = new Date();
        //填充地址信息
        String provinceName = districtService.getNameByCode(address.getProvinceCode());
        String cityName = districtService.getNameByCode(address.getCityCode());
        String areaName = districtService.getNameByCode(address.getAreaCode());
        address.setProvinceName(provinceName);
        address.setCityName(cityName);
        address.setAreaName(areaName);
        //日志更新
        address.setModifiedUser(username);
        address.setModifiedTime(date);

        addressMapper.updateByAid(address);

    }

    @Override
    public Address getByAid(Integer aid, Integer uid) {
        // 根据收货地址数据id，查询收货地址详情
        Address address = addressMapper.findByAid(aid);

        if (address == null) {
            throw new AddressNotFoundException("尝试访问的收货地址数据不存在");
        }
        if (!address.getUid().equals(uid)) {
            throw new AccessDeniedException("非法访问");
        }
        address.setProvinceCode(null);
        address.setCityCode(null);
        address.setAreaCode(null);
        address.setCreatedUser(null);
        address.setCreatedTime(null);
        address.setModifiedUser(null);
        address.setModifiedTime(null);
        return address;
    }
}